<template>
  <a-modal
    :title="model && model.id ? '编辑' : '新增'"
    :width="900"
    :height="360"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleOK"
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" style="display: none">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="店铺名称">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '请输入店铺名称' }] }]" />
        </a-form-item>
        <a-form-item label="店铺名称logo">
          <a-upload
            name="file"
            list-type="picture-card"
            class="avatar-uploader"
            accept="image/png, image/jpeg"
            :show-upload-list="false"
            :action="uploadApi"
            @change="handleChange"
            v-decorator="[
              'logo',
              {
                valuePropName: 'file',
                getValueFromEvent: normFile,
                rules: [{ required: true, message: '请上传图片' }],
              },
            ]"
          >
            <img
              v-if="fileList && fileList[0]"
              :src="fileList[0].response.data.src"
              alt="avatar"
              width="80"
              height="80"
            />
            <div v-else>
              <a-icon type="plus" />
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
          <a-icon class="loading" type="loading" v-if="logoLoading" />
          <div class="tips">建议尺寸: 45 * 45</div>
        </a-form-item>
        <a-form-item label="联系电话">
          <a-input v-decorator="['mobile', { rules: [{ required: true, message: '请输入店主电话' }] }]" />
        </a-form-item>                
        <a-form-item label="店铺介绍">
          <a-textarea :rows="3" v-decorator="['note', { rules: [{ required: true, message: '请输入店铺介绍' }] }]" />
        </a-form-item>
        <a-form-item label="状态">
          <a-radio-group
            name="radioGroup"
            v-decorator="['state', { rules: [{ required: true, message: '请选择状态' }] }]"
          >
            <a-radio :value="1"> 激活 </a-radio>
            <a-radio :value="2"> 冻结 </a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import api from '@/api/banner'
import WangEditor from '@/components/Editor/WangEditor.vue'

// 表单字段
const fields = ['name', 'id', 'mobile', 'state', 'note', 'logo']
const baseApi = process.env.VUE_APP_API_BASE_URL
export default {
  components: { WangEditor },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
    }
    return {
      form: this.$form.createForm(this),
      uploadApi: baseApi + api.upload,
      fileList: [],
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      if (this.model) {
        this.fileList = [
          {
            response: { data: { src: this.model.logo } },
            uid: '-' + this.model.id,
            name: this.model.id,
            status: 'done',
          },
        ]
        this.form.setFieldsValue(pick(this.model, fields))
      }
    })
  },
  methods: {
    normFile(e) {
      if (e.file.response && e.file.response.data) {
        // this.form.setFieldsValue({ srcSave: e.file.response.data.srcSave })
        return e.file.response.data.src
      }
      return ''
    },
    handleChange(info) {
      if (info.file.status === 'uploading') {
        this.bannerLoading = true
        return
      }
      if (info.file.status === 'done') {
        this.fileList = info.fileList.slice(-1)
        this.bannerLoading = false
      }
    },
    handleOK() {
      this.$emit('ok')
    },
  },
}
</script>
<style scoped>
.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}

.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
